package battle 
{
	import Common.GameDefine;
	import Common.GameNetManager;
	
	import Common.MapDirection;
	import map.MapGrid;
	import map.MapPos;
	import map.PathFinder;
	import Common.GameAction;
	
	
	
	
	/**
	 * ...
	 * @author ...
	 */
	public class GameCreatureBattleMove
	{
		public static const NORMALSPEED:Number = 0.8;
		public static const HURTLSPEED:Number = 0.1;
		public static const FALLSPEED:Number = 0.07;
		
		public var acceleration:Number = 0.0;
		public var speed:Number = NORMALSPEED;
		
		public var distance:Number = 0.0;
		public var sinX:Number = 0.0;
		public var cosX:Number = 0.0;
		
		public var pos:BattlePos = null;
		public var movePos:BattlePos = null;
		public var startPos:BattlePos = null;
		
		private var owner:Object = null;
		private var time:Number = 0.0;
		
		public var moving:Boolean = false;
		
		public var moveDirection:int = MapDirection.DIRECTCOUNT;
		public var changeAction:Boolean = true;
		
		public var overFunction:Function = null;
		
		public function GameCreatureBattleMove() 
		{
			
		}
		
		public function init( owner:Object ):void
		{
			this.owner = owner;
			
			pos = new BattlePos();
			movePos = new BattlePos();
			startPos = new BattlePos();
		}
		
		public function release():void
		{
			owner = null;
			overFunction = null;
			
			pos = null;
			movePos = null;
			startPos = null;
		}
		
		public function endMoving():void
		{
			if ( changeAction ) 
			{
				owner.state = GameCeatureState.STAND;
				owner.setAction( GameAction.STAND , null );
			}
			
			moving = false;
			
			pos.x = movePos.x;
			pos.y = movePos.y;
			
			startPos.x = movePos.x;
			startPos.y = movePos.y;
			
			overFunction( owner );
		}
		
		public function setPos( x:int , y:int ):void
		{
			startPos.x = x;
			startPos.y = y;
			
			pos.x = x;
			pos.y = y;
		}
		
		public function setMovePos( x:int , y:int , fun:Function , b:Boolean = true ):void
		{
			overFunction = fun;
			changeAction = b;
			
			movePos.x = x;
			movePos.y = y;
			
			distance = Math.sqrt( ( movePos.x - startPos.x ) * ( movePos.x - startPos.x ) + ( movePos.y - startPos.y ) * ( movePos.y - startPos.y ) );
			var a:int = Math.atan( ( movePos.y - startPos.y ) / ( movePos.x - startPos.x ) );
			sinX = ( movePos.y - startPos.y ) / distance;
			cosX = ( movePos.x - startPos.x ) / distance;
			
			
			var x:int = movePos.x - startPos.x;
			var y:int = movePos.y - startPos.y;
			
			if ( x > 0 && y > 0 ) 
				moveDirection = MapDirection.SOUTH;
			else if ( x > 0 && y == 0 ) 
				moveDirection = MapDirection.EAST;
			else if ( x > 0 && y < 0 ) 
				moveDirection = MapDirection.NORTH;
			else if ( x == 0 && y < 0 ) 
				moveDirection = MapDirection.NORTH;
			else if ( x < 0 && y < 0 ) 
				moveDirection = MapDirection.NORTH;
			else if ( x < 0 && y == 0 ) 
				moveDirection = MapDirection.WEST;
			else if ( x < 0 && y > 0 ) 
				moveDirection = MapDirection.SOUTH;
			else if ( x == 0 && y > 0 ) 
				moveDirection = MapDirection.SOUTH;
			
		}
		
		
		public function startMoving():void
		{
			owner.state = GameCeatureState.MOVE;
			
			owner.setAction( GameAction.MOVE , null );
			
			updateDirection();
			
			moving = true;
			
			time = 0;
		}
		
		private function updateDirection():void
		{
			
		}
		
		public function update( delay:Number ):void
		{
			if ( !moving ) 
			{
				return;
			}
			
			time += delay;
			var spd:Number = speed + delay * acceleration * 0.001;
			if ( spd < FALLSPEED ) 
			{
				spd = FALLSPEED;
			}
			
			var dis:Number = time * spd;
			
			pos.x = cosX * dis + startPos.x;
			pos.y = sinX * dis + startPos.y;
			
			if ( dis >= distance ) 
			{
				endMoving();
			}
		}
		
		
	}
	
	
}